import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox

class CurrencyConverter(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('人民币美元汇率转换')
        self.setGeometry(100, 100, 300, 200)

        # 创建布局
        layout = QVBoxLayout()

        # 创建标签和输入框
        self.label = QLabel('请输入人民币金额:', self)
        self.input_field = QLineEdit(self)
        self.result_label = QLabel('美元金额:', self)

        # 创建按钮
        self.convert_button = QPushButton('转换', self)
        self.convert_button.clicked.connect(self.convert_currency)

        # 将控件添加到布局中
        layout.addWidget(self.label)
        layout.addWidget(self.input_field)
        layout.addWidget(self.convert_button)
        layout.addWidget(self.result_label)

        # 设置布局
        self.setLayout(layout)

    def convert_currency(self):
        try:
            amount = float(self.input_field.text())
            rate = self.get_exchange_rate()
            converted_amount = amount / rate
            self.result_label.setText(f'美元金额: {converted_amount:.2f}')
        except ValueError:
            QMessageBox.warning(self, '输入错误', '请输入有效的数字')
        except Exception as e:
            QMessageBox.warning(self, '错误', str(e))

    def get_exchange_rate(self):
        # 1美元 = 7.28人民币
        return 7.28

if __name__ == '__main__':
    app = QApplication(sys.argv)
    converter = CurrencyConverter()
    converter.show()
    sys.exit(app.exec_())
